<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/31
 * Time: 10:39
 */
namespace app\admin\controller;

use app\admin\model\Admin as adminModel;
use app\admin\model\AuthGroup as authGroupModel;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule as authRuleModel;

class Admin extends Common {

    public function managers(){
        return view('managers',['title'=>'用户管理']);
    }

    public function rule(){
        $autuTree = (new authRuleModel)->authRuleTree();
        return view('rule',['title'=>'权限规则','autuTree'=>$autuTree]);
    }

    public function group(){
        return view('group',['title'=>'用户组管理']);
    }

    /**获取用户信息
     * @param $page
     * @param $limit
     */
    public function getUserList($page,$limit){
        if(request()->isAjax()){
            $result = adminModel::getAdminAll($page,$limit);
            $data =  $result['data']->toArray();
            foreach ($data['data'] as $k=>$v){
                foreach ($v['groups']  as $kk=>$vv){
                    $arr[] = $vv['title'];
                }
                $data['data'][$k]['user_group'] = implode('，',$arr);
            }
            $count = $result['count'];
            ajaxReturn(['code'=>0,'msg'=>'获取成功','data'=>$data['data'],'count'=>$count]);
        }
    }


    /**
     * 编辑后台用户信息
     */
    public function editUserInfo(){
        if(request()->isAjax()){
            $post = input('post.');
//            print_r($post);die;
            $nowUid = $post['data']['userId'];
            $groupID = AuthGroupAccess::getGroupIdByUid($nowUid)->toArray();
//            print_r($groupID);die;
            if($groupID){
               $del_res =  AuthGroupAccess::delGroupIdByUid($nowUid);
            }
//            $post['data']['status'] = 0;
            $uid = $post['data']['userId'];
            $i = 0;
            foreach ($post['data'] as $k=>$v) {
                if($k == 'status' && $v == 'on'){
                    $post['data']['status'] = 1;
                }
                if(is_integer($k)){
                    $addData[$i]['u_id'] = $uid;
                    $addData[$i]['group_id'] = $k;
                    $i ++;
                }
            }
            $data['u_id'] = $post['data']['userId'];
            $data['name'] = $post['data']['name'];
            $data['status'] = isset($post['data']['status']) ? $post['data']['status'] : 0;
//            dump([$addData,$data]);die;
            $res1 = AuthGroupAccess::addAuthGroupAccess($addData);
            $res2 = adminModel::updateAdminInfo($data);
            if($res1 && $res2){
                ajaxReturn(['code'=>0,'msg'=>'编辑成功','data'=>[]]);
            }else{
                ajaxReturn(['code'=>401,'msg'=>'编辑失败','data'=>[]]);
            }

        }
    }


    /**
     * 获取用户组信息
     * @param $page
     * @param $limit
     */
    public function getUserGroup($page,$limit){
        if(request()->isAjax()){
            $result = authGroupModel::getRuleGroupAll($page,$limit);
            $data =  $result['data']->toArray();
            $count = $result['count'];
            ajaxReturn(['code'=>0,'msg'=>'获取成功','data'=>$data['data'],'count'=>$count]);
        }
    }


    /**
     * 新增用户组信息
     */
    public function addUserGroup(){
        if(request()->isAjax()){
            $post = input('post.');
            $title = $post['title'];
            $status = isset($post['status']) && $post['status'] == 'on' ? 1 : 0;
            $checkAuth = [];
            if(isset($post['checkAuth'])){
                foreach ($post['checkAuth'] as $k=>$v){
                    $checkAuth[] = $v['id'];
                }
                $checkAuthStr = implode(',',$checkAuth);
            }else{
                ajaxReturn(['code'=>400,'msg'=>'请选择权限','data'=>[],'count'=>'']);
            }
            $data = [
                'title'=>$title,
                'status'=>$status,
                'rules'=>$checkAuthStr
            ];
            $res = authGroupModel::addUserGroup($data);
            if($res){
                ajaxReturn(['code'=> 0,'msg'=>'添加成功','data'=>[],'count'=>'']);
            }else{
                ajaxReturn(['code'=>401,'msg'=>'添加失败','data'=>[],'count'=>'']);
            }
        }
    }


    /**
     *获取用户规则权限
     */
    public function getAuthRuleTree(){
        if(request()->isAjax()){
            $autuTree = (new authRuleModel)->authRuleTree();
            ajaxReturn(['code'=> 0,'msg'=>'获取成功','data'=>$autuTree,'count'=>'']);
        }
    }

    /**
     * 新增规则权限
     */
    public function addUserRule(){
        if(request()->isAjax()){
            $post = input('post.');
            $module = $post['data']['module'];
            $controller = $post['data']['controller'];
            $action = $post['data']['action'];
            if(empty($module) && empty($controller) && empty($action)){
                $name = '#';
            }else{
                $name = $post['data']['module'] . '/' . $post['data']['controller'] .'/'.$post['data']['action'];
            }
            $pid = $post['data']['pid'];
            $typesd =  db('auth_rule')->where('id','=',$pid)->field('type')->find();
            if($typesd){
                $type =  $typesd['type'] + 1;
                if($type > 3){
                    ajaxReturn(['code'=>400,'msg'=>'只能添加三级','data'=>[],'count'=>'']);
                }
            }else{
                $type = 1;
            }
            $data = [];
            if($post){
                $data = [
                    'name'=> $name,
                    'title'=> $post['data']['title'],
                    'status' => isset($post['data']['status']) && $post['data']['status'] == 'on' ? 1 : 0,
                    'type'=> $type,
                    'pid' => $pid
                ];
            }
//        print_r($data);die;
            $res = authRuleModel::addRule($data);
            if($res){
                ajaxReturn(['code'=> 0,'msg'=>'添加成功','data'=>[],'count'=>'']);
            }else{
                ajaxReturn(['code'=>401,'msg'=>'添加失败','data'=>[],'count'=>'']);
            }
        }
    }


}